<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>医院管理系统 - 登录</title>
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
        }
        .login-container {
            background: white;
            border-radius: 15px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }
        .login-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 2rem;
            text-align: center;
        }
        .login-form {
            padding: 2rem;
        }
        .form-control {
            border-radius: 10px;
            border: 2px solid #e9ecef;
            padding: 12px 15px;
        }
        .form-control:focus {
            border-color: #667eea;
            box-shadow: 0 0 0 0.2rem rgba(102, 126, 234, 0.25);
        }
        .btn-login {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            border-radius: 10px;
            padding: 12px;
            font-weight: 600;
            transition: transform 0.2s;
        }
        .btn-login:hover {
            transform: translateY(-2px);
        }
        .register-link {
            color: #667eea;
            text-decoration: none;
        }
        .register-link:hover {
            color: #764ba2;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-6 col-lg-4">
                <div class="login-container">
                    <div class="login-header">
                        <i class="fas fa-hospital fa-3x mb-3"></i>
                        <h3>医院管理系统</h3>
                        <p class="mb-0">请登录您的账户</p>
                    </div>
                    <div class="login-form">
                        <form id="loginForm">
                            <div class="mb-3">
                                <label for="account" class="form-label">
                                    <i class="fas fa-user me-2"></i>账号
                                </label>
                                <input type="text" class="form-control" id="account" name="account" required>
                            </div>
                            <div class="mb-3">
                                <label for="password" class="form-label">
                                    <i class="fas fa-lock me-2"></i>密码
                                </label>
                                <input type="password" class="form-control" id="password" name="pw" required>
                            </div>
                            <div class="d-grid">
                                <button type="submit" class="btn btn-primary btn-login">
                                    <i class="fas fa-sign-in-alt me-2"></i>登录
                                </button>
                            </div>
                        </form>
                        <div class="text-center mt-3">
                            <p class="mb-0">还没有账户？ 
                                <a href="/register" class="register-link">立即注册</a>
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="/js/bootstrap.bundle.min.js"></script>
    <script src="/js/axios.min.js"></script>
    <script>
        // 配置axios基础URL
        axios.defaults.baseURL = '';
        
        // 登录表单提交
        document.getElementById('loginForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const account = document.getElementById('account').value;
            const password = document.getElementById('password').value;
            
            // 显示加载状态
            const submitBtn = document.querySelector('button[type="submit"]');
            const originalText = submitBtn.innerHTML;
            submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin me-2"></i>登录中...';
            submitBtn.disabled = true;
            
            try {
                console.log('发送登录请求:', { account, pw: password });
                const response = await axios.post('/user/login', {
                    account: account,
                    pw: password
                });
                
                console.log('登录响应:', response.data);
                
                if (response.data.code === 200) {
                    // 存储JWT令牌
                    localStorage.setItem('token', response.data.msg);
                    localStorage.setItem('userInfo', JSON.stringify(response.data.data));
                    
                    console.log('用户信息已保存:', response.data.data);
                    console.log('用户信息类型:', typeof response.data.data);
                    console.log('用户信息键:', Object.keys(response.data.data || {}));
                    
                    // 根据角色跳转
                    const userRole = response.data.data.urole;
                    console.log('用户角色:', userRole, '类型:', typeof userRole);
                    
                    if (userRole === 2 || userRole === '2') {
                        console.log('跳转到管理员首页');
                        // alert('登录成功！正在跳转到管理员首页...');
                        window.location.href = '/admin/home';
                    } else if (userRole === 1 || userRole === '1') {
                        console.log('跳转到医生首页');
                        // alert('登录成功！正在跳转到医生首页...');
                        window.location.href = '/doctor/home';
                    } else if (userRole === 0 || userRole === '0') {
                        console.log('跳转到患者首页');
                        // alert('登录成功！正在跳转到患者首页...');
                        window.location.href = '/patient/home';
                    } else {
                        console.error('未知的用户角色:', userRole);
                        console.error('完整用户数据:', response.data.data);
                        console.error('用户数据的所有字段:', Object.keys(response.data.data || {}));
                        console.error('用户数据值:', JSON.stringify(response.data.data, null, 2));
                        alert('用户角色错误，请联系管理员。角色值: ' + userRole + '\n完整数据: ' + JSON.stringify(response.data.data, null, 2));
                    }
                } else {
                    console.error('登录失败:', response.data);
                    alert('登录失败：' + response.data.msg);
                }
            } catch (error) {
                console.error('登录错误:', error);
                if (error.response) {
                    console.error('错误响应:', error.response.data);
                    alert('登录失败：' + (error.response.data.msg || error.response.data.message || '服务器错误'));
                } else if (error.request) {
                    console.error('请求错误:', error.request);
                    alert('登录失败，请检查网络连接');
                } else {
                    console.error('其他错误:', error.message);
                    alert('登录失败：' + error.message);
                }
            } finally {
                // 恢复按钮状态
                submitBtn.innerHTML = originalText;
                submitBtn.disabled = false;
            }
        });
    </script>
</body>
</html>
